// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include "commoncppproblem23.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=23 lang=cpp
 *
 * [23] 合并K个升序链表
 *
 * https://leetcode-cn.com/problems/merge-k-sorted-lists/description/
 *
 * algorithms
 * Hard (56.61%)
 * Likes:    1857
 * Dislikes: 0
 * Total Accepted:    429K
 * Total Submissions: 756.4K
 * Testcase Example:  '[[1,4,5],[1,3,4],[2,6]]'
 *
 * 给你一个链表数组，每个链表都已经按升序排列。
 *
 * 请你将所有链表合并到一个升序链表中，返回合并后的链表。
 *
 *
 *
 * 示例 1：
 *
 * 输入：lists = [[1,4,5],[1,3,4],[2,6]]
 * 输出：[1,1,2,3,4,4,5,6]
 * 解释：链表数组如下：
 * [
 * ⁠ 1->4->5,
 * ⁠ 1->3->4,
 * ⁠ 2->6
 * ]
 * 将它们合并到一个有序链表中得到。
 * 1->1->2->3->4->4->5->6
 *
 *
 * 示例 2：
 *
 * 输入：lists = []
 * 输出：[]
 *
 *
 * 示例 3：
 *
 * 输入：lists = [[]]
 * 输出：[]
 *
 *
 *
 *
 * 提示：
 *
 *
 * k == lists.length
 * 0 <= k <= 10^4
 * 0 <= lists[i].length <= 500
 * -10^4 <= lists[i][j] <= 10^4
 * lists[i] 按 升序 排列
 * lists[i].length 的总和不超过 10^4
 *
 *
 */

// @lc code=start

//   Definition for singly-linked list.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/* struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
}; */

class Solution
{
public:
    struct cmp
    {
        bool operator()(ListNode *p1, ListNode *p2)
        {
            return p1->val > p2->val ? true : false;
        }
    };

    ListNode *mergeKLists(vector<ListNode *> &lists)
    {
        if (lists.empty())
        {
            return nullptr;
        }
        cout<<"链表非空"<<endl;
        priority_queue<ListNode *, vector<ListNode *>, cmp> q;
        for(int i=0;i<lists.size();++i){
            cout<<"处理第 "<<i<<" 个链表"<<endl;
            ListNode* p=lists[i];
            while(p!=nullptr){
                q.push(p);
                p=p->next;
                cout<<"push success"<<endl;
            }
        }
        /* for (ListNode *list : lists)
        {
            if (list)
            {
                q.push(list);
            }
        } */
        ListNode *head = new (ListNode);
        ListNode *p = head;
        while (!q.empty())
        {
            p->next = q.top();
            q.pop();
            cout<<"pop "<<p->next->val<<"success"<<endl;
            p = p->next;
            /* if (p->next)
            {
                q.push(p->next);
            } */
        }
        p->next=nullptr;
        //  ListNode* newhead=head->next;
        // delete(head); 
        return head->next;
        // return newhead;
    }
};
// @lc code=end
